//table
function ctrlInitArrowTree(context, selector, options){
	var context = $(selector,context);
	
	var init = function(){
		
		$(".ctrl-tree-toolbar .ctrl-action-tree-open", context).click(function(){
			var browser = $(this).parents(".ctrl-browser");
			CtrlAjax.changeStateVars(browser.length?browser:this, {},{})
			return false;
			var t = $.tree.focused();
			t.open_all();
			return false;
		});		
		
		$(".ctrl-tree-toolbar .ctrl-action-create" , context).click(function(e){
			e.preventDefault()
			var t = $.tree.focused(); 
			var type = t.selected.attr("rel");
			var settings = t.settings.types[type];
			if(t.selected){ 
				if(options.addTemplate || ( settings && settings.add_template ) ){
					
					var max_children = settings.max_children;
					if(max_children == 0){
						alert("Do tego elementu nie można nic dodać")
						return false;
					}
					if(settings.add_template == ""){
						t.create();
						return false;
					}
						
					var id = t.selected.find("a").attr("href");
					
					var li = t.selected;
					var size = options.editSize.split("|");
					if(settings.add_template != null){
						var url =  settings.add_template.replace("#key#", id);
						size = settings.add_dialog_size.split("|");
					}else{
						var url =  options.addTemplate.replace("#key#", id);
					}
					url = $.query.load(url).set("type", type).set("iframe", 1).toString();
					var dialog = tmt.jquery.modalframe.open(url, 
						{ 
							height: parseInt(size[1]), 
							width: parseInt(size[0]),
							onClose: function(){
								var browser = $(li).parents(".ctrl-browser");
								CtrlAjax.changeStateVars(browser.length?browser:li, {},{}) 
							}
						} 
					);
				}else{
					t.create();
				}
			}
			else 
				alert("Wybierz katalog nadrzędny");
				
			return false;
		});
		
		$(".ctrl-tree-toolbar .ctrl-action-delete", context).click(function(){
			var t = $.tree.focused(); 
			if(t.selected) 
				t.remove(); 
			else 
				alert("Wybierz węzeł do usunięcia");
			return false;
		});
		
		
		var tmpLinkSave = false;
		$(".ctrl-tree-toolbar .ctrl-action-save", context).mousedown(function(){
			var t = $.tree.focused();
			if(t.selected){
				if(!tmpLinkSave){
					tmpLinkSave = $(this).attr("href");
				}
				var folderId = $(t.selected).find("a").eq(0).attr("href"); 
				$(this).attr("href", tmpLinkSave.replace("#id#", folderId));
				return false;
			}else{
				alert("Wybierz katalog do edycji");
				event.preventDefault();
				event.stopImmediatePropagation();
				return false;
			}
		});	
		
		if(options.disableInit == 1)
			return false;
		
		if(options.multiSelect == 1){
			
			$(".ctrl-tree-inner", context).tree({
				ui : {
					theme_name : "checkbox"
				},
				plugins : { 
					checkbox : {  three_state : false }
				},
				callback:{
					
					onchange : function (el) {
						
					 	var checked = $.tree.plugins.checkbox.get_checked();
					 	
					 	var ids = new Array();
					 	for(var i =0; i< checked.length; i++){
					 		var elId = $("a",checked[i]).eq(0).attr("href");
					 		ids.push(elId);
					 	}
						$("input", context).val(ids.join(";"));
						if (options.onSelect) window[options.onSelect]();
						return true;
					}
				}
	
			});
			
			var val = $("input", context).val();
			if(val){
				var ids = val.split(";");
			
				var tree = $.tree.reference( $(".ctrl-tree-inner", context)  );
				for(var i=0; i< ids.length; i ++){
					var links = tree.container.find("[href="+ids[i]+"]").eq(0);
					var li = links.parent()
					if(links.length > 0){
						$.tree.plugins.checkbox.check(li);
						try{
						tree.parent(li).removeClass("closed").addClass("open");
							//do sprawdzenia
						tree.parent(li).parents("li").removeClass("closed").addClass("open");
						}catch(e){ }
					}
				}
			}
			
		}else{
			
		
			$(".ctrl-tree-toolbar .ctrl-action-save", context).mousedown(function(){
				$(this).attr("rel" ,"400|300|"+"pages-list-ajax");
			});
			
			
			$(".ctrl-tree-inner", context).tree({
				ui:{
					theme_name: 'classic'
				},
				plugins : { 
					cookie: { prefix : "ctrl_jstree_" + options.id  },
					contextmenu : { 
						
						items : {
							create : {
								label	: "Dodaj folder"
							},
							edit: {
								label: "Edytuj",
								action	: function (NODE, TREE_OBJ) { 
									var folderId = $(NODE).find("a").eq(0).attr("href");
									tmt.jquery.modalframe.open(data.editLink + "&iframe=1&id="+folderId,
										{width: 800, height: 500,
										close: function(event, ui) { 
											CtrlAjax.setStateVars( NODE,{} );
										
										}
										}
									);
								},
								icon	: "rename"
								
							},
							rename : {
								label	: "Zmień nazwę", 
								icon	: "rename"
							},
							remove : {
								label	: "Usuń",
								icon	: "remove"
							}
						}
					}
				},
				types: options.types,
				callback:{
					onselect : function (el) {
						var nodeId = $(el).find("a").eq(0).attr("href");
						if (options.onSelect) window[options.onSelect](nodeId);
					},
					onchange : function (el) {
						var elId = $(el).find("a").eq(0).attr("href");
						$("input", context).val(elId);
					},
					beforedelete: function(NODE, TREE_OBJ){
						var treeId = $(NODE).find("a").eq(0).attr("href");
						if($(NODE).hasClass("root")){
							alert("Nie możesz usunąć tego katalogu.");
							return false;
						}
						
						if(confirm("Czy napewno chcesz usunąć '"+TREE_OBJ.get_text(NODE)+"'?")){
							CtrlAjax.doAction( $(".ctrl-tree-inner", context).parents(".ctrl-ajax").eq(0) ,options.model,"delete",treeId, 
								{}, 
								function(response){}
							);
							return true;
						}
						
					},
					onmove : function (NODE,REF_NODE,TYPE,TREE_OBJ,RB) {
						var nodeId = $("a",NODE).attr("href");
						var targetId = 1;
						if(TYPE == "inside"){
							targetId = $("a",REF_NODE).attr("href");
							if(targetId != undefined){
								CtrlAjax.doAction( $(".ctrl-tree-inner", context).parents("td") ,options.model,"save", nodeId, 
									{ parent_id:targetId }, 
									function(response){
										if(response.singleResponse == undefined) {
											return false;
										}
									}
								);
							}				
						}else{
							targetId = $(REF_NODE).parents("li:eq(0)").find("a:eq(0)").attr("href");
							var moveRelId =  $(REF_NODE).find("a:eq(0)").attr("href");
							if(targetId != undefined && moveRelId != undefined){
								CtrlAjax.doAction( $(".ctrl-tree-inner", context).parents("td") ,options.model,"move", nodeId, 
									{ parent_id:targetId }, 
									function(response){
										if(response.singleResponse == undefined) {
											return false;
										}
									},
									{refId: moveRelId, parent_id:targetId, insertType: TYPE }
								);
							}
							
							
							return false;
						}
						if(targetId != undefined){
							return false;
						}
					},
					onrename: function(NODE, TREE_OBJ, RB){
						var folderId = $(NODE).find("a").eq(0).attr("href");
						var name = TREE_OBJ.get_text(NODE);
						
						
						
						if(folderId != ""){
							CtrlAjax.doAction( $(".ctrl-tree-inner", context).parents("td") ,options.model,"save", folderId, 
									{ name:name }, 
									function(response){
										
									}
							);
							return;
						}
						
						
						
						var name = TREE_OBJ.get_text(NODE);
						var parent = TREE_OBJ.parent(NODE).find("a").eq(0).attr("href");
						var nod = NODE;
						var clazz = $(context).parents(".ctrl-ajax").eq(0).attr('class');
						var selector = clazz.split(' ').slice(-1) + "";
						selector = selector.replace("ctrl-ajax-","");
						obj = new Object();
						obj["parent_id"] = parent;
						obj[options.structureField] = name;
						CtrlAjax.doAction( $(".ctrl-tree-inner", context).parents(".ctrl-ajax").eq(0) ,options.model,"create","", 
							obj, 
							function(response){
								$(nod).find("a").eq(0).attr("href", response.singleResponse);
								var getKey = "ctrl-tree-selected";
								var get = {};
								get[getKey] = response.singleResponse;
								CtrlAjax.changeStateVars( 
										selector, 
										{
											"selected" : response.singleResponse,
											"listOnly" : 1
										},
										get
								);
							}
						);
					}
				}
			});
		}
		
		
	
	
		

		
		
		$("li a span", context).click( function() { 
			if (options.onFiltr) {
				var nodeId = $(this).parent().attr("href");
				window[options.onFiltr](nodeId);
			}
			
			return false ; 
			
		} ) ;
	
	}
	
	if(context.is(":visible")){
		init();
	}else
		setTimeout(init, 1200);
	
}